package com.study.demo.ui.forum;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;

import com.stduy.injector.PerActivity;
import com.study.demo.data.ForumRepository;
import com.study.demo.db.Forum;
import com.study.demo.event.DelForumAttentionEvent;
import com.study.demo.service.OffLineService;
import com.study.demo.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;


/**
 * Created by sll on 2016/3/11.
 */
@PerActivity
public class ForumListPresenter implements ForumListContract.Presenter {

    private final ForumRepository mForumRepository;
    private final Context mContext;
    private final EventBus eventBus;

    private ForumListContract.View mForumListView;
    private Disposable disposable;

    @Inject
    public ForumListPresenter(ForumRepository mForumRepository, Context mContext, EventBus eventBus) {
        this.mForumRepository = mForumRepository;
        this.mContext = mContext;
        this.eventBus = eventBus;
    }

    @Override
    public void onForumListReceive(final String forumId) {
        mForumListView.showLoading();
        disposable = mForumRepository.getForumList(forumId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(forumList -> {
                            if (forumList == null || forumList.isEmpty()) {
                                mForumListView.onError();
                            } else {
                                mForumListView.hideLoading();
                                mForumListView.renderForumList(forumList);
                            }
                        }, throwable ->
                                throwable.printStackTrace()
                );
    }

    @Override
    public void onForumAttentionDelClick(final Forum forum) {
        mForumRepository.removeForum(forum.getFid())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(aBoolean -> {
                    if (aBoolean != null && aBoolean) {
                        ToastUtil.showToast("取消关注成功");
                        eventBus.post(new DelForumAttentionEvent(forum.getFid()));
                        mForumListView.removeForum(forum);
                    }
                }, throwable -> ToastUtil.showToast("取消关注失败，请重试"));
    }

    @Override
    public void onForumOfflineClick(Forum forum) {
        Intent intent = new Intent(mContext, OffLineService.class);
        ArrayList<Forum> forums = new ArrayList<>();
        forums.add(forum);
        intent.putExtra(OffLineService.EXTRA_FORUMS, forums);
        intent.setAction(OffLineService.START_DOWNLOAD);
        mContext.startService(intent);
    }

    @Override
    public void onForumClick(Forum forum) {
        mForumListView.showThreadUi(forum.getFid());
    }

    @Override
    public void attachView(@NonNull ForumListContract.View view) {
        mForumListView = view;
    }

    @Override
    public void detachView() {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
        mForumListView = null;
    }
}
